package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.view.categories.SKUCategoriesView;
import com.superman.supermarket.client.view.categories.SKUCategoriesView.SKUCategoriesViewPresenter;

/**
 * 商品分类
 * 
 * @author superman
 * @version 2018年8月2日下午5:08:48
 */
public class SKUCategoriesActivity extends ShopAbstractActivity implements SKUCategoriesViewPresenter {

	@Inject
	SKUCategoriesView skuCategoriesView;

	@Override
	public void getChildCateListByCateId(String cid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("UPPER_ID", cid);
		postman.post("catalog/children/list", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				JSONObject jo = JSONParser.parseStrict(response).isObject();
				if (jo.get("body").isObject().get("count").isNumber().doubleValue() > 0) {
					skuCategoriesView.refreshChildCategoryList(jo.get("body").isObject().get("list").isArray());
				} else {
					skuCategoriesView.showNoChildCategoryList();
				}
			}
		});
	}

	@Override
	public IPageView getView() {
		return skuCategoriesView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		return this;
	}

	@Override
	public void onActivity(EventBus eventBus) {
	}

	@Override
	public void popToRefresh() {
		// TODO Auto-generated method stub
	}

	@Override
	public void popToRefresh(Object contextData) {
		// TODO Auto-generated method stub

	}

	@Override
	public void pushToRefresh() {
		postman.post("catalog/top/list", null, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				JSONObject jo = JSONParser.parseStrict(response).isObject();
				skuCategoriesView.refreshTopCategoryList(jo.get("body").isObject().get("list").isArray());
			}
		});
	}

}
